package edu.rmit.service.impl;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListModel;

import edu.rmit.client.LiveChatPanel;
import edu.rmit.service.IChatCallBackService;

/**
 * This interface implements the chat callback operation. Which can implement
 * send chat message, send forum message, and remove forum message.
 * 
 * @author Joey George
 * @version 1.0
 */
@SuppressWarnings("serial")
public class CallBackServiceImpl extends UnicastRemoteObject implements
		IChatCallBackService {

	/**
	 * The chat client object, which we can add the message to it.
	 */
	private LiveChatPanel chatClient;

	/**
	 * The contructor to initialize an chat client object.
	 * 
	 * @param chatClient
	 * @throws RemoteException
	 */
	public CallBackServiceImpl(LiveChatPanel chatClient) throws RemoteException {
		this.chatClient = chatClient;
	}

	/**
	 * This method is to implement add a message to message list with the new
	 * value.
	 * 
	 * @param listMessage
	 *            A list which you want to add an message to a list.
	 * @param newValue
	 *            The new value which you want to add.
	 */
	private void addMessageList(JList listMessage, String newValue) {
		DefaultListModel listModel = new DefaultListModel();
		ListModel currentListModel = listMessage.getModel();
		for (int i = 0; i < currentListModel.getSize(); i++) {
			listModel.addElement(listMessage.getModel().getElementAt(i));
		}
		listModel.addElement(newValue);
		listMessage.setModel(listModel);
	}

	/**
	 * This method is to implement remove a message to message list with the new
	 * value.
	 * 
	 * @param listMessage
	 *            A list which you want to remove an message to a list.
	 * @param messageId
	 *            The message id which you want to removed.
	 */
	private void removeMessageList(JList listMessage, int messageId) {
		DefaultListModel listModel = new DefaultListModel();
		ListModel currentListModel = listMessage.getModel();
		for (int i = 0; i < currentListModel.getSize(); i++) {
			if (i != messageId) {
				listModel.addElement(listMessage.getModel().getElementAt(i));
			}
		}
		listMessage.setModel(listModel);
	}

	/**
	 * This method implements the operation to send an add chat message to all
	 * users.
	 * 
	 * @param from
	 *            The sender name of the chat message.
	 * @param message
	 *            The message body which the sender send.
	 * @throws RemoteException
	 */
	@Override
	public void addChatMessage(String from, String message)
			throws RemoteException {
		String messageWapper = from + ":" + message + "\n";
		JList listMessage = chatClient.getPanelLiveChat().getListMessage();
		addMessageList(listMessage, messageWapper);
	}

	/**
	 * This method implements the operation to send an add forum message to all
	 * users.
	 * 
	 * @param from
	 *            The sender name of the forum message.
	 * @param message
	 *            The message body which the sender send.
	 * @throws RemoteException
	 */
	@Override
	public void addForumMessage(String from, String message)
			throws RemoteException {
		String messageWapper = from + ":" + message + "\n";
		JList listMessage = chatClient.getPanelForum().getListMessage();
		addMessageList(listMessage, messageWapper);
	}

	/**
	 * This method implements the operation to send an joined message to all
	 * users.
	 * 
	 * @param from
	 *            The sender name of the chat message.
	 * @throws RemoteException
	 */
	@Override
	public void joinedChatMessage(String from) throws RemoteException {
		String messageWapper = from + " has joined.\n";
		JList listMessage = chatClient.getPanelLiveChat().getListMessage();
		addMessageList(listMessage, messageWapper);
	}

	/**
	 * This method implements the operation to send an quit message to all
	 * users.
	 * 
	 * @param from
	 *            The sender name of the chat message.
	 * @throws RemoteException
	 */
	@Override
	public void quitChatMessage(String from) throws RemoteException {
		String messageWapper = from + " has left.\n";
		JList listMessage = chatClient.getPanelLiveChat().getListMessage();
		addMessageList(listMessage, messageWapper);
	}

	/**
	 * This method implements the operation to send an system message to all
	 * users.
	 * 
	 * @param message
	 *            The message body which the system want to send.
	 * @throws RemoteException
	 */
	@Override
	public void systemInfoMessage(String message) throws RemoteException {
		String messageWapper = "*System*:" + message + " \n";
		JList listMessage = chatClient.getPanelLiveChat().getListMessage();
		addMessageList(listMessage, messageWapper);
	}

	/**
	 * This method implements the operation to send an remove forum message to
	 * all users.
	 * 
	 * @param messageId
	 *            The message id which you have removed.
	 * @throws RemoteException
	 */
	@Override
	public void removeForumMessage(int messageId) throws RemoteException {
		JList listMessage = chatClient.getPanelForum().getListMessage();
		removeMessageList(listMessage, messageId);
	}

}
